# Austin Laugesen
# This python script demonstrates how to connect to a website, login and retrieve HTML.
# We can parse this HTML for our component testing.

# Necessary libraries for pulling info from the Internet
import cookielib, urllib2, re

# Get a handle on the session information stored in cookies.
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

# This is the URL from which we want to retrieve HTML.
login_url = 'http://www.alaug.com/website/authentication/index.php'
#login_url = 'http://localhost/~Austin/macrohard/website/authentication/index.php'

# Information we want to pass in a POST so we successfully login.
# this gets appended to the end of a URL, for example:
# http://www.alaug.com/website/authentication/index.php?email=invalid@live.com&password=invalid&submit=submit
# http://localhost/~Austin/macrohard/website/authentication/index.php?email=invalid@gmail.com&password=invalid&submit=submit


''' ######## INPUTS ########'''
login_form = 'email=man@live.com&password=man&submit=submit'

# Below is where the login occurs! We're passing the proper POST
# to the authentication page, retrieving HTML and storing it in the variable 'page'
page = opener.open(login_url, login_form)

# print the contents of the page we connected to in the console
htmlPulledFromServer = page.read()

'''######## OUTPUTS ########'''
output = 'Austin'

####### SEARCH FOR OUTPUTS
# Go through HTML to verify the right information is displayed.
m = re.search(output, htmlPulledFromServer)

### As a tester we only care about when m == None, this means the test failed! :(
if None == m:
	# TODO - write the error to a file... w/ the format: <timestamp> <URL> <name of test that failed>
	print 'didn\'t find ' + output
else:
	print 'found ' + output

# close our connection with the current page
page.close()
